package com.xiaobai.netease_news.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.model.Editor;
import com.xiaobai.netease_news.model.ImgExtra;
import com.xiaobai.netease_news.model.ResponseData;
import com.xiaobai.netease_news.utils.ImageUniversalLoaderConfiguration;
import com.xiaobai.netease_news.utils.ViewHolder;

import java.util.List;

/**
 * Created by Android Studio
 * Project: NeteaseNews
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/5
 */
public class NewsListAdapter extends BaseAdapter {

    private List<ResponseData> responseDatas;
    private LayoutInflater inflater;
    private Context context;

    private DisplayImageOptions options = ImageUniversalLoaderConfiguration.initOptions();

    public NewsListAdapter(Context context, List<ResponseData> responseDatas) {
        this.context = context;
        this.responseDatas = responseDatas;
        if (context != null) {
            inflater = LayoutInflater.from(context);
        }
    }

    @Override
    public int getCount() {
        if (responseDatas != null) {
            Log.d("responseDatas", String.valueOf(responseDatas.size()));
            return responseDatas.size();
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        ResponseData data = responseDatas.get(position);

        if (data.getImgExtras() != null && data.getImgExtras().size() > 0)
            return 1;
        else if (data.getImgType() == 1)
            return 3;
        else
            return 2;
    }

    @Override
    public ResponseData getItem(int position) {
        if (responseDatas != null) {
            return responseDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//        Log.d("pos", String.valueOf(position));
        ResponseData responseData = getItem(position);

        List<ImgExtra> responseDataImgExtras = responseData.getImgExtras();
        int imgType = responseData.getImgType();
        if (convertView == null) {
            if (responseDataImgExtras != null && responseDataImgExtras.size() > 0)
                convertView = inflater.inflate(R.layout.news_hot_pic_layout, parent, false);
            else if (imgType == 1)
                convertView = inflater.inflate(R.layout.news_img_type1_layout, parent, false);
            else
                convertView = inflater.inflate(R.layout.news_normal_layout, parent, false);
        }

        ImageView commonNewsImg = ViewHolder.getView(convertView, R.id.common_news_img);
        TextView commonNewsTitle = ViewHolder.getView(convertView, R.id.common_news_title);
        TextView commonNewsDigest = ViewHolder.getView(convertView, R.id.common_news_digest);
        TextView commonNewsReplyCount = ViewHolder.getView(convertView, R.id.common_news_reply_count);

        TextView imgType1Title = ViewHolder.getView(convertView, R.id.item_img_type1_news_title);
        ImageView imgType1Img = ViewHolder.getView(convertView, R.id.item_img_type1_img);
        ImageView editorImg = ViewHolder.getView(convertView, R.id.item_editor_img);
        TextView editorName = ViewHolder.getView(convertView, R.id.item_editor_name);
        TextView imgType1Digest = ViewHolder.getView(convertView, R.id.item_img_type1_digest);
        TextView imgType1ReplyCount = ViewHolder.getView(convertView, R.id.item_img_type1_reply_count);

        TextView hotPicTitle = ViewHolder.getView(convertView, R.id.hot_pic_news_title);
        TextView hotPicReplyCount = ViewHolder.getView(convertView, R.id.hot_pic_reply_count);
        ImageView hotPicImg1 = ViewHolder.getView(convertView, R.id.hot_pic_img1);
        ImageView hotPicImg2 = ViewHolder.getView(convertView, R.id.hot_pic_img2);
        ImageView hotPicImg3 = ViewHolder.getView(convertView, R.id.hot_pic_img3);

        if (responseDataImgExtras != null && responseDataImgExtras.size() > 0) {
            hotPicTitle.setText(responseData.getTitle());
            int replyCount = responseData.getReplyCount();
            hotPicReplyCount.setText(String.valueOf(replyCount) + "更贴" +
                    (responseData.getTag() == null ? "" : responseData.getTag()));

            ImageLoader.getInstance().displayImage(responseData.getImgsrc(), hotPicImg1, options);
            ImageLoader.getInstance().displayImage(responseDataImgExtras.get(0).getImgsrc(), hotPicImg2, options);
            ImageLoader.getInstance().displayImage(responseDataImgExtras.get(1).getImgsrc(), hotPicImg3, options);
        } else if (imgType == 1) {
            List<Editor> editors = responseData.getEditors();
            String title = responseData.getTitle();
            imgType1Title.setText(title);
            ImageLoader.getInstance().displayImage(responseData.getImgsrc(), imgType1Img, options);

            if (editors != null && editors.size() > 0) {
                ImageLoader.getInstance().displayImage(editors.get(0).getEditorImg(), editorImg, options);
                editorName.setText(editors.get(0).getEditorName());
            } else {
                editorImg.setVisibility(View.GONE);
                editorName.setVisibility(View.GONE);
            }

            imgType1Digest.setText(responseData.getDigest());
            imgType1ReplyCount.setText(responseData.getReplyCount() + "跟帖");
        } else {
            ImageLoader.getInstance().displayImage(responseData.getImgsrc(), commonNewsImg, options);
            commonNewsTitle.setText(responseData.getTitle());
            commonNewsDigest.setText(responseData.getDigest());
            commonNewsReplyCount.setText(responseData.getReplyCount() + "跟帖");
        }

        return convertView;
    }
}
